#ifndef _SFS_NAME_SVR_H_
#define _SFS_NAME_SVR_H_

#include <string>

#include <log4cxx/logger.h>
#include <json/json.h>

#include "ServerFrameWork.h"
#include "CnfSvrClient.h"

using std::string;

using namespace log4cxx;

class NameSvr {

	public:
		int init(const char * log_conf_file, const char * cs_ip, unsigned short cs_port, int cs_conn_pool_size, \
				int cs_socket_conn_timeout, int cs_socket_send_timeout, int cs_socket_recv_timeout, \
				int cs_socket_max_response_size);

		int start();

	protected:
		static int handle_socket_input_func(int fd, void * arg);

		int dispatch_request(int cmd_id, json_object * j, string & response);

		int handle_create_file(json_object * j, string & response);

		int handle_delete_file(json_object * j, string & response);

		int handle_create_dir(json_object * j, string & response);

		int handle_delete_dir(json_object * j, string & response);

		int handle_get_file_metadata_info(json_object * j, string & response);

		int handle_alloc_mds_4_new_file(json_object * j, string & response);
		
		int handle_query_is_file_exist(json_object * j, string & response);
		int handle_query_is_dir_exist(json_object * j, string & response);
		int handle_read_dir(json_object * j, string & response);

		string build_file_metadata_svr_record(int mds_id, int dir_id, int file_id);

		int get_file_metadata_record(const char * file_path, int32_t & mds_id, int32_t & dir_id, int64_t & file_id);

	private:
		static LoggerPtr _logger;
		string _listen_ip;
		unsigned short _listen_port;
		int _thread_num;
		string _log_conf_file;
		int _socket_idle_timeout;
		int _socket_send_timeout;
		int _socket_recv_timeout;
		int _max_request_size;
		string _root_data_path;
		ServerFrameWork _sfw;
		CnfSvrClient _csc;
};

#endif
